<?php
namespace app\cdyoa\controller;
use think\Db;
class Usernote extends Base
{   
    public function lists(){
        if( is_post() ){
            $page     = input('page');
            $keyword  = input('keyword');
            $status  = input('status');
            $type    = input('type');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            $sql = "
                SELECT
                    [1]
                FROM
                    in_shou a
                left join
                    admin_user b
                on
                    a.user_id = b.id
                left join
                    admin_user c
                on
                    a.action_user_id = c.id
                left join
                    admin_user d
                on
                    a.bang_user_id=d.id
                left join
                    orders e
                on
                    a.orders_id=e.id
                WHERE 
                    1 = ?
                ";
            $param = [1];
            if ( !empty($keyword) ) {
                $sql .= " and (a.note like ?)";
                $param[] = "%{$keyword}%";
            }
            if ( $type != '100all' ) {
                $sql .= " and a.type = ?";
                $param[] = $type;
            }
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( !empty($min_time) ) {
                $sql    .= " and a.shou_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and a.shou_time <= ?";
                $param[] = strtotime($max_time);
            }
            //分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
            $page_data   = com_page_data($total, $page,  $page_size);
            
            //列表
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by a.id desc limit $start, $page_size";
            $field       = "a.*,b.name, c.name as aname,d.name as bname,e.title,e.status as orders_status";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);
                $list[$k]['type']           =  c('type.inshou_type',$v['type']);
                $list[$k]['status']           =  c('type.inshou_status',$v['status']);
                $list[$k]['create_time']    =  com_format_time( $v['create_time'] );
                $list[$k]['shou_time']    =  com_format_time( $v['shou_time'] );
                $list[$k]['money']          =  number_format($v['money'], 2);
                $list[$k]['rate']          =  number_format($v['rate'], 6);
                $list[$k]['rmb']          =  number_format($v['rmb'], 2);
                
                //操作按钮
                $list[$k]['action']="";
                if($v['orders_status']!=3){
                    $list[$k]['action']=com_page(['ico'=>'edit','title'=>'编辑汇率','url'=>url('inshou/edit','id='.$v['id']),'width'=>800,'height'=>600]);
                }
                if($v['status']==0){
                    $list[$k]['action'].=com_page(['ico'=>'del3','title'=>'删除收款','url'=>url('inshou/delete'), 'post'=>['id'=>$v['id'],'t'=>'in_shou'],'confirm'=>"你确定要删除吗"]);
                }
            }
            $com_body    = com_build_table($list, array("id","name", "type" ,"money","rate","rmb", "note","bname","title","aname","action_note", "shou_time", "status","create_time","action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data]);
        }
        $data = [
            'nav'       => ['计划订单列表', '列表'],
            'type'      =>  ['100all'=>'全部币种'] + c('type.inshou_type'),
            'status'    =>  ['100all'=>'全部状态'] + c('type.inshou_status'),
            
        ];
        return view('', $data);
    }
    
    //绑定收款
    public function bangdin($id=''){
        $info = Db::name('orders')->find($id);
        if( is_post() ){
            $page     = input('page');
            $keyword  = input('keyword');
            $type    = input('type');
            $min_time = input('min_time');
            $max_time = input('max_time');
            
            $sql = "
                SELECT
                    [1]
                FROM
                    in_shou
                WHERE 
                    orders_id is null and 1=?
                ";
            $param = [1];
            if ( !empty($keyword) ) {
                $sql .= " and (note like ?)";
                $param[] = "%{$keyword}%";
            }
            if ( $type != '100all' ) {
                $sql .= " and type = ?";
                $param[] = $type;
            }
            if ( !empty($min_time) ) {
                $sql    .= " and shou_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and shou_time <= ?";
                $param[] = strtotime($max_time);
            }
            //分页
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            $page_size   = c('page_size');
            $page        = com_cur_page($total, $page,  $page_size);
            $page_data   = com_page_data($total, $page,  $page_size);
            //列表
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by id desc limit $start, $page_size";
            $field       = "*";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['type']           =  c('type.inshou_type',$v['type']);
                $list[$k]['shou_time']    =  com_format_time( $v['shou_time'] );
                $list[$k]['money']          =  number_format($v['money'], 2);
                $list[$k]['rate']          =  number_format($v['rate'], 6);
                $list[$k]['rmb']          =  number_format($v['rmb'], 2);
                //操作按钮
                $list[$k]['action']=com_page(['ico'=>'shenhe-tongguo','title'=>'绑定订单','url'=>url('inshou/bdin'),'post'=>['id'=>$v['id'],'orders_id'=>$info['id']],'confirm'=>'您确定要绑定该收款到计划订单【'.$info['title'].'】吗?']);
            }
            $com_body    = com_build_table($list, array("shou_time","type" ,"money","rate","rmb", "note" ,"action"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            com_check(['data'=>$data]);
        }
        $data = [
            'info'      => $info,
            'type'      =>  ['100all'=>'全部币种'] + c('type.inshou_type'),
            
        ];
        return view('', $data);
    }
    
    //绑定收款
    public function bdin(){
        if(is_post()){
            $id = input('id');
            $rs = Db::name('in_shou')->find($id);
            if( $rs['status']!=0 || $rs['orders_id']){
                    com_error(['已绑定过了,不能进行二次绑定！']);
            }else{
                $d = [
                    'id' => $id,
                    'orders_id' => input('orders_id'),
                    'status' => 1,
                    'bang_user_id' => $this->userinfo['id'],
                ];
                Db::name('in_shou')->update($d);
            }
            com_check(['suc'=>['js_action'=>'alert|close','msg'=>'订单绑定收款成功，请等待审核！']]);
        }
    }

    //添加
    public function add(){
        return "暂未开放";
        if( is_post() ){
            $data = com_data(input());
            $rule = [
                'shou_time' =>  'require',
                'type'      =>  'require',
                'money'      =>  'require|float',
                //'rate'      =>  'require|float',
                //'rmb'      =>  'require|float',
            ];
            $msg = [];
            com_validate( $rule, $msg, $data, true );
            $data['user_id']   = $this->userinfo['id'];
            $data['create_time'] = time();
            $data['shou_time'] = strtotime($data['shou_time']);
            Db::name('in_shou')->insert($data);
            $config = [
                'msg' => '添加成功！',
                'suc' => [
                    'url' => url('publics/suc'),
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        } 
        $d = [
            'nav'       => ['收款入账', '列表'],
            'type'      => c('type.inshou_type'),
        ];
        return view('',$d);
    }
    
    //编辑
    public function edit($id){
        if( is_post() ){
            $data = [
                'rate' => input('rate'),
                'rmb'  => input('rmb'),
                'note'  => input('note'),
                'id'   => $id, 
            ];
            $rule = [
                'rate'      =>  'require|float',
                'rmb'     =>  'require|float',
            ];
            $msg = [];
            com_validate( $rule, $msg, $data, true );
            $data['update_time'] = time();
            Db::name('in_shou')->update($data);
            $config = [
                'msg' => '修改汇率成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $d = [
            'type'      => c('type.inshou_type'),
            'info'      => Db::name('in_shou')->find($id),
            'id'  => $id,
        ];
        return view('',$d);
    }
    
    public function delete(){
        if(is_post()){
            $id = input('id');
            $status  = Db::name('in_shou')->where('id',$id)->value('status');
            if( $status!=0 ){
                com_error(['已绑定订单,不能删除']);
            }else{
                Db::name('in_shou')->delete( $id );
            }
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check( $config );
        }
    }
}
